#pragma once

#include<string>
#include<algorithm>
#include<iostream>
#include<assert.h>
#include<string.h>

using namespace std;

namespace xyx
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}		

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

        void swap(const vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_end_of_storage, v._end_of_storage);
        }

        T& operator=(vector<T> v)
        {
            swap(v);
            return *this;
        }

        void resize(size_t n, const T& val = T())
        {
            if(n < size())
            {
                _finish = _start + n;
            }
            else
            {
                reserve(n);
                while(_finish != _start + n)
                {
                    *_finish = val;
                    ++_finish;
                }
            }
        }
       
        vector(size_t n, const T& val = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            resize(n,val);
        }
        

        vector(int n, const T& val = T())
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            resize(n,val);
        }
        
        template<class Inputiterator>
        vector(Inputiterator first, Inputiterator last)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storage(nullptr)
        {
            while(first!=last)
            {
                push_back(*first);
                ++first;
            }
        }

        //传统写法
        vector(const vector<T>& v)
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
        {
            T* tmp = new T[v.capacity()];
            //memcpy(tmp,v._start, sizeof(T) * v.size());
            
            for(size_t i = 0; i < size(); i++)
            {
                tmp[i] = _start[i];
            }
            _start = tmp;
            _finish = _start + v.size();
            _end_of_storage = _start + v.capacity();
        }

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
		{}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		void pop_back()
		{
			erase(end());
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return *(_start + pos);
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return *(_start + pos);
		}

		void push_back(const T& x)
		{
			//判断容量，不够就扩容
			//if (_finish == _end_of_storage)
			//{
			//	size_t oldcapacity = capacity();
			//	size_t newcapacity = oldcapacity == 0 ? 4 : 2 * oldcapacity;
			//	reserve(newcapacity);
			//}
			////尾插数据
			//*_finish = x;
			//++_finish;

			//直接复用insert
			insert(end(), x);
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);

			if (_finish == _end_of_storage)
			{
				//提前记录偏移量，避免迭代器失效
				size_t len = pos - _start;

				size_t oldcapacity = capacity();
				size_t newcapacity = oldcapacity == 0 ? 4 : 2 * oldcapacity;
				reserve(newcapacity);
				
				//更新pos
				pos = _start + len;
			}

			//_finish - 1 表示最后一个有效位置
			iterator end = _finish - 1;
			//挪动数据
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}

			//插入数据，并++_finish
			*pos = x;
			++_finish;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos <= _finish);
			
			iterator it = pos + 1;
			while (it < end())
			{
				*(it - 1) = *it;
				++it;
			}

			_finish--;
			return pos;
		}

		void reserve(size_t n)
		{
			//避免delete后，size()为0
			size_t oldsize = size();
			//大于n才扩容
			if (n > capacity())
			{
				T* tmp = new T[n];
				//_start 不为空拷贝数据
				if (_start)
				{
                    //这里使用memcpy会导致string类这样的深拷贝的类出现问题
                    //因为memcpy会进行值拷贝
					//memcpy(tmp, _start, sizeof(T) * oldsize);
                    for(size_t i = 0; i < oldsize; i++)
                    {
                        tmp[i] = _start[i];
                    }
					delete[] _start;
				}
				//更新成员变量
				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storage = _start + n;
			}
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}
        
        

	private:
		iterator _start;//起始位置
		iterator _finish;//尾插数据位置
		iterator _end_of_storage;//极限容量位置
	};
    void test_01()
    {
        xyx::vector<int> v; 
        v.resize(10);
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }

    void test_02()
    {
        xyx::vector<int> v;
        v.push_back(3);
        v.push_back(5);
        v.push_back(1);
        v.push_back(7);
        v.push_back(9);

        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;

        xyx::vector<int> v1(v);

        for(auto& e : v1)
        {
            cout << e << " ";
        }
        cout << endl;

        xyx::vector<int> v2 = v1;


        for(auto& e : v2)
        {
            cout << e << " ";
        }
        cout << endl;


    }
    void test_03()
    {
        
        xyx::vector<int> v(10,1);

        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        
        int a[] = {1,3,5,7};
        xyx::vector<int> v1(a,a+4);

        for(auto& e : v1)
        {
            cout << e << " ";
        }
        cout << endl;
       
        string str("hello world");
        vector<char> v2(str.begin(),str.end());

        for(auto& e : v2)
        {
            cout << e << " ";
        }
        cout << endl;

    }
}
